﻿package com.tomvian.twodee.sideon.interactions 
{
	import flash.display.DisplayObject;
	import com.tomvian.twodee.common.actions.Action;
	import com.tomvian.twodee.common.interactions.Interaction;
	import com.tomvian.twodee.sideon.SideOnActor;
	import com.tomvian.maths.Vector2D;
	
	/**
	 * Blocks
	 * @author Thomas Vian
	 */
	public class Blocks extends Interaction
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		public var pushing:Action;
		
		public var friction:Number;
		public var maxMove:Number;
		
		protected var _currentBlock:DisplayObject;
		protected var _pushingRight:Boolean;
		
		protected var _originalTestPoint:Vector2D;
		protected var _move:Number;
		
		//--------------------------------------------------------------------------
		//
		//  Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function Blocks(blocks:Array, friction:Number, maxMove:Number, pushPoint:Vector2D = null) 
		{
			super(blocks, pushPoint);
			
			this.friction = friction;
			this.maxMove = maxMove;
			
			_originalTestPoint = new Vector2D();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		override public function interact():void 
		{
			if (enabled)
			{
				_testPoint.copy(actorMass.position);
				_testPoint.addMod(cameraOffset).addMod(actorMass.velocity);
				_testPoint.y += interactionPoint.y;
				
				if (actor.direction == 1) 		_testPoint.x += interactionPoint.x;
				else if(actor.direction == 3)	_testPoint.x -= interactionPoint.x;
				
				wasInteracting = interacting;
				interacting = false;
				
				for (var i:uint = 0, l:uint = interactiveObjects.length; i < l; i++)
				{
					var block:DisplayObject = interactiveObjects[i];
					
					if (block.hitTestPoint(_testPoint.x, _testPoint.y, true))
					{
						interacting = true;
						
						if (!wasInteracting)
						{
							currentObject = block;
							
							if (pushing) actor.currentAction = pushing;
						}
						
						_originalTestPoint.copy(_testPoint);
						
						if (actor.direction == 1)
						{
							_testPoint.x -= 0.1;
							while (block.hitTestPoint(_testPoint.x, _testPoint.y, true)) _testPoint.x -= 0.1;
							
							_move = _originalTestPoint.x - _testPoint.x;
							_move *= 0.7;
							
							if (_move > maxMove)
							{
								block.x += maxMove;
								actorMass.position.x -= _move - maxMove;
							}
							else
							{
								block.x += _move;
							}
						}
						else if (actor.direction == 3)
						{
							_testPoint.x += 0.1;
							while (block.hitTestPoint(_testPoint.x, _testPoint.y, true)) _testPoint.x += 0.1;
							
							_move = _testPoint.x - _originalTestPoint.x;
							_move *= 0.7;
							
							if (_move > maxMove)
							{
								block.x -= maxMove;
								actorMass.position.x += _move - maxMove;
							}
							else
							{
								block.x -= _move;
							}
						}
						
						actorMass.friction.x *= friction;
						
						break;
					}
				}
			}
		}	
	}
}